﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Windows.Forms;
using NetJsWire.JsonRpcHandler.Attributes;
using NetJsWire.Widgets.Renderers.Core;

namespace NetJsWire.Widgets.Renderers.ToolBar
{
    [ClassName( "Ext.Toolbar.Button" )]
    internal class Button : Widget
    {
        private readonly Ui.ToolBar.Button _bi;
        protected string _CLICK = "function() {{ SendToServer(\"{1}\", \"Click\", \"{1}\", null); }}";
        protected string _CLICK_CLIENT_NAME = "click";

        protected string _TOGGLE = "NetJsWire.ToggleHandler";
        protected string _TOGGLE_CLIENT_NAME = "toggle";

        public Button( object baseObject )
            : base( baseObject )
        {
            _bi = ( Ui.ToolBar.Button ) baseObject;
        }

        public override void Render()
        {
            if ( !Rendered )
            {
                SetAttribute( "isInContainer", true );
                EnsureParentNodePosition();

                //SetConstructionProperty( "xtype", "button" );
                SetConstructionProperty( "text", _bi.Text );
                SetConstructionProperty( "toolTip", _bi.ToolTipText );

                SetAttribute( ControlNode, "idx", _bi.Owner.Items.IndexOf( _bi ) );

                if ( _bi.Image != null &&
                     ( _bi.DisplayStyle == ToolStripItemDisplayStyle.ImageAndText ||
                       _bi.DisplayStyle == ToolStripItemDisplayStyle.Image ) )
                {
                    if ( !string.IsNullOrEmpty( _bi.Text ) &&
                         ( _bi.DisplayStyle == ToolStripItemDisplayStyle.Text ||
                           _bi.DisplayStyle == ToolStripItemDisplayStyle.ImageAndText ) )
                        SetConstructionProperty( "cls", "x-btn-text-icon" );
                    else
                        SetConstructionProperty( "cls", "x-btn-icon" );

                    if ( string.IsNullOrEmpty( _bi.IconCls ) )
                        SetConstructionProperty( "icon", "webapp/Images.ashx?_id=" + InternalID );
                }
                else if ( !string.IsNullOrEmpty( _bi.IconCls ) )
                {
                    SetConstructionProperty( "iconCls", _bi.IconCls );
                }

                if ( _bi.CheckOnClick )
                {
                    SetConstructionProperty( "enableToggle", true );

                    if ( _bi.Checked )
                    {
                        SetConstructionProperty( "pressed", true );
                    }

                    _bi.Toggle += _bi_Toggle;
                }
            }
            base.Render();
        }

        void _bi_Toggle( object sender, EventArgs e )
        {
            _bi.Checked = Convert.ToBoolean( sender );
        }
    }
}